package com.texas_android.app.Appconfig;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;

import com.texas_android.app.http.bean.perssion.PerssionBean;
import com.texas_android.app.utils.Logger;

import java.util.ArrayList;

/**
 * Created by solin on 2017/11/21.
 */

public class AppContext extends MyApplication {

    public static ArrayList<Activity> activities = new ArrayList<>();

    public static PerssionBean bean;

    public static PerssionBean getBean() {
        return bean;
    }

    public static void setBean(PerssionBean bean) {
        AppContext.bean = bean;
    }

    public static void addActivity(Activity activity) {
        activities.add(activity);
    }

    public static void closeActivityAll() {
        for (Activity activity : activities) {
            if (activity != null) {
                activity.finish();
                activities.remove(activity);
            }
        }
    }

    public static DisplayMetrics getDisplayMetrics(Context context) {
        return context.getResources().getDisplayMetrics();
    }

    /**
     * 获取手机屏幕高度
     *
     * @param context
     * @return
     */
    public static float getScreenHeight(Context context) {
        return getDisplayMetrics(context).heightPixels;
    }

    /**
     * 获取手机屏幕宽度
     *
     * @param context
     * @return
     */
    public static float getScreenWidth(Context context) {
        return getDisplayMetrics(context).widthPixels;
    }

    /**
     * ping
     *
     * @param pingNum ping的次数
     * @param url     ping的目标URL
     * @return
     */
    public static boolean ping(int pingNum, String url) {
        int status = 0;
        Process p;
        try {
            p = Runtime.getRuntime().exec("/system/bin/ping -c " + pingNum + " " + url);
            status = p.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (status == 0) {
            Logger.log("网络连接通");
            return true;
        } else {
            Logger.log("网络连接不通");
            return false;
        }
    }


}
